{{extend 'layout.html'}}
{{include 'filter_hosts.html'}}

{{=XML(add.formModal())}}
<div id="accounts_list">
    <div id="accountsbtns" class="navbar btn-toolbar">
        {{=XML(add.btn_show())}}
        <a id="delete_account" class="btn btn-small"><i class="icon-remove"></i> Delete</a>
        <a id="reload_account" class="btn btn-small" onclick="javascript:accounttable.fnReloadAjax(); return false;"><i class="icon-refresh"></i> Reload</a>
        <a id="compromised" class="btn btn-small" onclick="javascript:send_compromise(); return false;"><i class="icon-tag"></i> Compromise/Uncompromise</a>
        <a id="import_account" class="btn btn-small"><i class="icon-upload"></i> Import File</a>
        <a id="paste_account" class="btn btn-small"><i class="icon-pencil"></i> Paste File</a>
        <a id="show_pwdump" class="btn btn-small"><i class="icon-user"></i> Show PWDUMP</a>
        <a id="filter_hosts" class="btn btn-small" data-toggle="modal" href="#filterModal"><i class="icon-filter"></i> Filter</a>
    </div>

    {{=form}}
</div>

<script type="text/javascript" language="javascript">

function send_compromise() {
    var ids = dt_checkboxer_select_DT_RowIds(accounttable);
    if (ids == '') { alert('Nothing selected'); return false; }
    var data = "ids=" + ids;
    var url = "{{=URL('accounts', 'compromise.json', user_signature=True)}}";
    $.web2py.ajax_page( 'POST', url, data, 'undefined' );
}

var accounttable;

jQuery(document).ready(function() {

    jQuery("#delete_account").click(function(e) {
        e.preventDefault();

        //bootbox.options.onEscape =
        bootbox.confirm('These items will be permanently deleted and cannot be recovered. Are you sure?', "Cancel", "Delete", function(confirmed) {
            if(confirmed) {
                var checks = dt_checkboxer_select_DT_RowIds(accounttable);
                if (checks == '') { alert('Nothing selected'); return false; }
                var data = "ids=" + checks;
                url = "{{=URL('accounts', 'delete.json', user_signature=True)}}"
                $.web2py.ajax_page( 'POST', url, data, 'undefined' );
            }
        });
    });

    var dialog_import_account = jQuery('<div></div>').appendTo('body');

    jQuery("#import_account").click(function(e) {
        e.preventDefault();

        dialog_import_account.load("{{=URL('accounts', 'import_file.load')}}");
        jQuery('#t_accounts_f_services_id').select2();

        bootbox.confirm(dialog_import_account, "Cancel", "Submit", function(confirmed) {
            if(confirmed) {
                var import_obj = jQuery('#accounts_import_form');
                var import_action = import_obj.attr('action').replace('.load', '.json');
                import_obj.get(0).setAttribute('action', import_action);
                ajax_form_submit(import_obj);
            }
        }).addClass('bigModal');
    });

    var dialog_paste_account = jQuery('<div></div>').appendTo('body');

    jQuery("#paste_account").click(function(e) {
        e.preventDefault();

        dialog_paste_account.load("{{=URL('accounts', 'paste.load')}}");
        jQuery('#t_accounts_f_services_id').select2();

        bootbox.confirm(dialog_paste_account, "Cancel", "Submit", function(confirmed) {
            if(confirmed) {
                var data = jQuery('#accounts_paste_form').serialize();
                var url = "{{=URL('accounts', 'paste.json')}}";
                $.web2py.ajax_page( 'POST', url, data, 'undefined' );
            }
        }).addClass('bigModal');
    });

    jQuery('#show_pwdump').click( function() {
        var pwdumptext='';
        var rows;
        rows = accounttable.$('tr', {'filter':'applied'}).toArray();
        rows.forEach( function(node) {
            if (node.cells[7].innerText == 'LM') {
                pwdumptext += $(node).find('#username').text() + ':' +
                             node.cells[3].innerText + ':' +
                             node.cells[7].innerText + ':' +
                             node.cells[9].innerText + ':::\n';
            }
        } );
        
        bootbox.alert('<button id="btnpwdump" class="btn btn-small" title="Copy" data-clipboard-target="pwdump_text" data-clipboard-text="">Copy</button></br><pre id="pwdump_text">'+pwdumptext+'</pre>').addClass('bigModal');
    } );

    accounttable = $('#accounttable').dataTable( {
        "bServerSide": true,
        "sAjaxSource": "{{=URL('accounts', 'list.json')}}/" + location.search,
        //"sPaginationType": "full_numbers",
        "aoColumnDefs": [
            //{ "bSortable": false, "aTargets": [ 0 ] },
            //{ "bSortable": false, "aTargets": [ 2 ] },
            //{ "bVisible": false, "aTargets": [ 0 ] },
        ],
        "sDom": 'TC<"clear">lfrtip',
        "fnDrawCallback": function() {
            jQuery('#accounttable tbody tr td:nth-child(7)').css("color", "red").css("font-weight", "bold"); // passwords are red
        },
        "oTableTools": {
            "sSwfPath": "{{=URL('static','TableTools/swf/copy_csv_xls.swf')}}",
            "sRowSelect": "multi",
            "sSelectedClass": "row_selected",
            "aButtons": [
                {
                    "sExtends":  "collection",
                    "sButtonText": "Select",
                    "aButtons": [
                        {
                            "sExtends": "text",
                            "sButtonText": "All",
                            "fnClick": function( nButton, oConfig, oFlash ) {
                                jQuery(accounttable.fnGetFilteredNodes()).addClass('row_selected');
                            }
                        },
                        {
                            "sExtends": "text",
                            "sButtonText": "None",
                            "fnClick": function( nButton, oConfig, oFlash ) {
                                jQuery(accounttable.fnGetFilteredNodes()).removeClass('row_selected');
                            }
                        },
                        {
                            "sExtends": "text",
                            "sButtonText": "Compromised",
                            "fnClick": function( nButton, oConfig, oFlash ) {
                                jQuery(accounttable.fnGetFilteredNodes()).removeClass('row_selected');
                                jQuery("#accounttable .acct_compromised").each(function() {
                                    jQuery(this).parent().parent().addClass('row_selected');
                                });
                            }
                        },
                        {
                            "sExtends": "text",
                            "sButtonText": "Uncompromised",
                            "fnClick": function( nButton, oConfig, oFlash ) {
                                jQuery(accounttable.fnGetFilteredNodes()).removeClass('row_selected');
                                jQuery("#accounttable .acct_uncompromised").each(function() {
                                    jQuery(this).parent().parent().addClass('row_selected');
                                });
                            }
                        }

                    ]
                },
                "copy",
                "csv",
                "print"
             ]
        },
    } );

    accounttable.fnSetFilteringDelay(520);

    $('.bootbox').on('show', function () {
        $(this).find('.modal-body').css({
            'overflow-y':'scroll', 'width':'auto', 'height':'auto'
        });
    });
} );

</script>

